package com.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.web.context.WebApplicationContext;

import com.dao.CustomerDAO;
import com.model.Customer;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.utility.ApplicationContextUtil;

public class CustomerAction extends ActionSupport implements  ServletRequestAware,Preparable {	
	//
	private Customer customer=new Customer();
	private CustomerDAO customerDAO;
	List<Customer> savedCustomerList = new ArrayList<Customer>();
	List<String> itemList=new ArrayList<String>();
	List<String> genderList=new ArrayList<String>();
	private HttpServletRequest request;
	
	private String id;
	private String custName;
	private String address;
	private String gender;
	private String item;
	
	private String defaltItemValue;
	private String defaltGenderValue;
	private String defaultNamevalue;
	private String defaultAddress;	
	private String defaultIdKey;
	
    
	
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
		
	}	
		
	public List<Customer> getSavedCustomerList() {
		return savedCustomerList;
	}	

	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}	
		
	public List<String> getItemList() {
		return itemList;
	}
	
	
	public String getDefaltItemValue() {
		return defaltItemValue;
	}    	
	
	public List<String> getGenderList() {
		return genderList;
	}

	public void setGenderList(List<String> genderList) {
		this.genderList = genderList;
	}	
	
	public String getDefaltGenderValue() {
		return defaltGenderValue;
	}

	public void setDefaltGenderValue(String defaltGenderValue) {
		this.defaltGenderValue = defaltGenderValue;
	}
	
	public String getDefaultNamevalue() {
		return defaultNamevalue;
	}
	
	public String getDefaultAddress() {
		return defaultAddress;
	}	
	    	
	public String getDefaultIdKey() {
		return defaultIdKey;
	}
    
	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getCustName() {
		return custName;
	}


	public void setCustName(String custName) {
		this.custName = custName;
	}


	public String getAddress() {
		return address;
	}


	public void setAddress(String address) {
		this.address = address;
	}


	public String getGender() {
		return gender;
	}


	public void setGender(String gender) {
		this.gender = gender;
	}


	public String getItem() {
		return item;
	}


	public void setItem(String item) {
		this.item = item;
	}
    
	@Override
	public void prepare() throws Exception {
		itemList.add("Car");
		itemList.add("Laptop");
		itemList.add("Mobile");
		
		genderList.add("Male");
		genderList.add("Female");	
	}
	
    public void validate(){
		
		this.defaultIdKey=getId();
		
		if(getCustName()==null || getCustName().equals("")){
			addFieldError("custName","The name is required");
		}else{
			this.defaultNamevalue=getCustName();
		}
		
		if(getAddress()==null || getAddress().equals("") ){
			addFieldError("address","The address is required");
		}else{
			this.defaultAddress=getAddress();
		}
		
		if(getGender()==null || getGender().equals("")){
			addFieldError("gender","The gender is required");
		}else{
			this.defaltGenderValue=getGender();
		}
		
		if(getItem()==null || getItem().equals("")){
			addFieldError("item","The item is required");
		}else{
			this.defaltItemValue=getItem();
		}		
	}

	public String addCustomer() throws Exception {
		Customer customer=new Customer();
		customerDAO=(CustomerDAO)ApplicationContextUtil.getSpecifiedBean("customerDAOBean",this.request);
		
	    if(!getId().trim().equals(""))
	    	customer.setId(Long.parseLong(getId()));
	    customer.setCustName(custName);
	    customer.setAddress(address);
	    customer.setGender(gender);
	    customer.setItem(item);
	    
		customerDAO.saveOrUpdate(customer);		
		return "successView";
 	}
	
	public String editCustomer() throws Exception{
		customerDAO=(CustomerDAO)ApplicationContextUtil.getSpecifiedBean("customerDAOBean",this.request);
		Customer updateCustormer =customerDAO.getCustomerById(request.getParameter("id"));		
		
		this.defaltItemValue=updateCustormer.getItem();
		this.defaltGenderValue=updateCustormer.getGender();
		this.defaultNamevalue=updateCustormer.getCustName();
		this.defaultAddress=updateCustormer.getAddress();
		this.defaultIdKey=((Long)updateCustormer.getId()).toString();
		
		return "editInput";
	}
	
	@SkipValidation
	public String deleteCustomer() throws Exception{
		customerDAO=(CustomerDAO)ApplicationContextUtil.getSpecifiedBean("customerDAOBean",this.request);
		customerDAO.delete(request.getParameter("id"));
		savedCustomerList=customerDAO.fetchAllCustomer();
		return "successView";
	}
	
	@SkipValidation
	public String showAllCustomers() throws Exception{
		customerDAO=(CustomerDAO)ApplicationContextUtil.getSpecifiedBean("customerDAOBean",this.request);
		savedCustomerList=customerDAO.fetchAllCustomer();
		System.out.println("XXX");
		
		return "successView";
	}
			
	@SkipValidation
	public String populateAddCustomerinterface() throws Exception{			
		return "success";
	}	
}
